import { type Ref } from 'vue'
import { useToggle } from '@vueuse/core'

export function useSheetSelector(options: Ref<string[]>, checked: Ref<string[]>) {
  const [visible, toggle] = useToggle()

  let resolve: (v: string[]) => void
  const closeModal = () => {
    resolve([])
    toggle(false)
  }
  const resolveSelect = () => {
    resolve(checked.value)
    closeModal()
  }

  const selectSheet = async (sheets: string[]): Promise<string[]> => {
    toggle(true)
    checked.value = sheets
    options.value = sheets
    return new Promise<string[]>((res) => (resolve = res))
  }

  return {
    visible,
    closeModal,
    selectSheet,
    resolveSelect
  }
}
